﻿using System;// \r\n
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace Example
{
    class Example
    {
        static void Main()
        {
            StreamWriter writer = new StreamWriter("..\\..\\Alabala.cs", false, Encoding.GetEncoding("UTF-8"));
            //ako e false - nov fail, ako e true - Append(dobavq kym faila bez da go iztriva)
            //StreamWriter writer = new StreamWriter("..\\..\\Alabala.cs",false, Encoding.GetEncoding("windows-1251"));
            using (writer)
            {

                StreamReader reader = new StreamReader(@"..\..\Program.cs", Encoding.GetEncoding("UTF-8"));
                using (reader)
                {
                    int linenumber = 0;
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        linenumber++;
                        writer.WriteLine("Line {0} : {1}", linenumber, line);
                        line = reader.ReadLine();
                    }
                }
                Console.WriteLine("READY!");
            }
            Console.Read();
            StreamReader reader2 = new StreamReader(@"..\..\Program.cs");
            using (reader2)
            {
                StreamReader reader1 = new StreamReader("..\\..\\Alabala.cs");
                using (reader1)
                {
                    StreamWriter writer1 = new StreamWriter("..\\..\\Alabala1.cs");
                    using (writer1)
                    {
                        int linenumber = 0;
                        string line = reader1.ReadLine();
                        string linetosplit = line.ToString();
                        string linereader2 = reader2.ReadLine();
                        while (line != null)
                        {

                            linenumber++;
                            bool linetosplitbool = linetosplit.Contains("Line " + linenumber + " : " + linereader2);
                            string equal1 = ("Line " + linenumber + " : " + linereader2);
                            bool lineequals = String.Equals(linetosplit, equal1);
                            bool equal = equal1 == linetosplit;
                            string linenumbertostring = linenumber.ToString();
                            string lineexample = ("Line " + linenumbertostring + " : ");
                            writer1.WriteLine(line);
                            writer1.Flush();//tova e da prenebregne buferiraneto(kogato zavurshi izvejdaneto nacqlo togava zapisva
                            //faila), tuka pishe red po red vuv faila
                            Console.WriteLine(line);
                            try
                            {
                                line = reader1.ReadLine();
                                //if (line != null)
                                //{
                                linetosplit = line.ToString();
                                linereader2 = reader2.ReadLine();
                                //}
                            }
                            catch (NullReferenceException ex) // catch(IOException ex) // za cqloto InputOutputExceptions
                            {
                                Console.WriteLine("NullReferenceException - Veche e null linetosplit : " + ex.Message);
                            }
                        }
                    }
                }
            }
            try
            {
                StreamReader reader1 = new StreamReader("..\\..\\NotExcistingFile.txt");
            }
            catch (System.NullReferenceException exc)
            {
                Console.WriteLine("{0}", exc.Message);

            }
            catch (System.IO.FileNotFoundException)
            {
                Console.WriteLine("FILE NOT FOUND!");
            }
            catch
            {
                Console.WriteLine("Fatal error occured!");
            }

            StreamReader reader3 = new StreamReader(@"..\..\Program.cs");
            using (reader3)
            {
                int count = 0;
                string text = reader3.ReadToEnd();
                int index = text.IndexOf("foundme", 0);
                while (index!=-1) // ako ne e namereno shte vurne -1
                {
                    count++;
                    index = text.IndexOf("foundme", index + 1);
                }
                Console.WriteLine("foundme founded in {0} times",count);
            }
        }
    }
}